package com.vue.cn.service.impl;

import com.vue.cn.domin.Employee;
import com.vue.cn.domin.EmployeeExample;
import com.vue.cn.mapper.EmployeeMapper;
import com.vue.cn.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> findAll() {
        EmployeeExample example = new EmployeeExample();
        example.setOrderByClause("employee_id desc");
        return employeeMapper.selectByExample(example);
    }

    @Override
    public int deleteById(int id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int add(Employee employee) {
        return employeeMapper.insert(employee);
    }

    @Override
    public int edit(Employee employee) {
        return employeeMapper.updateByPrimaryKey(employee);
    }

    @Override
    public Employee findById(int id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public Employee findByEmail(String email) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmployeeEmailEqualTo(email);
        List<Employee> employees = employeeMapper.selectByExample(example);
        if (employees.size()==0) {
            return null;
        }
        return employees.get(0);
    }


}
